﻿using System.Collections.ObjectModel;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using Looptribe.Moodlin8.Model;

namespace Looptribe.Moodlin8.ViewModel
{
    public class CoursesViewModel : LoadingViewModel
    {
        public ICommand GotoCourseCommand { get; set; }

        private readonly ObservableCollection<CoursesCourseWrapper> _courses = new ObservableCollection<CoursesCourseWrapper>();
        public ObservableCollection<CoursesCourseWrapper> Courses
        {
            get { return _courses; }
        }

        public CoursesViewModel()
        {
            GotoCourseCommand = new RelayCommand<Course>(ExecuteGotoCourse);
        }

        private void ExecuteGotoCourse(Course course)
        {
            RunAsync(() => Controller.GotoCourse(course));
        }

        public async void LoadContent()
        {
            IsLoading = true;
            Courses.Clear();
            var courses = (await Api.GetEnrolledCoursesAsync(CurrentUser)).Data;
            IsLoading = false;
            if (courses == null)
                return;
            foreach (var course in courses)
                Courses.Add(new CoursesCourseWrapper(course));
        }
    }
}
